package com.tf_cut.admin.util;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.DateUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;

public class ExcelUtils {
	public static void exportExcel(HttpServletResponse response, String fileName, Collection<?> list,
			Class<?> pojoClass) throws IOException {
		if (StringUtils.isBlank(fileName)) {
			// 当前日期
			fileName = DateUtils.formatDate(new Date());
		}

		Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("商户名称", "申请记录"), pojoClass, list);
		Sheet sheet1 = workbook.getSheetAt(0);
		sheet1.setDefaultColumnWidth(50 * 256);
		sheet1.setDefaultRowHeight((short) (2 * 256));
		response.setCharacterEncoding("UTF-8");
		response.setHeader("content-Type", "application/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls");
		ServletOutputStream out = response.getOutputStream();
		workbook.write(out);
		out.flush();
		out.close();
	}

	public static void exportExcelToTarget(HttpServletResponse response, String fileName, Collection<?> sourceList,
			Class<?> targetClass) throws Exception {
		List<Object> targetList = new ArrayList<>(sourceList.size());
		for (Object source : sourceList) {
			Object target = targetClass.newInstance();
			BeanUtils.copyProperties(source, target);
			targetList.add(target);
		}
		exportExcel(response, fileName, targetList, targetClass);
	}

	public static void exportExcels(HttpServletResponse response, String fileName, Collection<?> list,
			Class<?> pojoClass,String sellerCompanyName) throws IOException {
		if (StringUtils.isBlank(fileName)) {
			// 当前日期
			fileName = DateUtils.formatDate(new Date());
		}

		Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(sellerCompanyName, "申请记录"), pojoClass, list);
		Sheet sheet1 = workbook.getSheetAt(0);
		sheet1.setDefaultColumnWidth(50 * 256);
		sheet1.setDefaultRowHeight((short) (2 * 256));
		response.setCharacterEncoding("UTF-8");
		response.setHeader("content-Type", "application/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls");
		ServletOutputStream out = response.getOutputStream();
		workbook.write(out);
		out.flush();
		out.close();
	}

	public static void exportExcelToTargets(HttpServletResponse response, String fileName, Collection<?> sourceList,
			Class<?> targetClass, String sellerCompanyName) throws Exception {
		List<Object> targetList = new ArrayList<>(sourceList.size());
		for (Object source : sourceList) {
			Object target = targetClass.newInstance();
			BeanUtils.copyProperties(source, target);
			targetList.add(target);
		}
		exportExcels(response, fileName, targetList, targetClass, sellerCompanyName);
	}

}
